/*
 * $Id: ProdiagsScheduleTask.java,v 1.1 2009/07/06 18:19:02 afaichun Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.common.prodiags;

import java.net.UnknownHostException;
import java.security.NoSuchAlgorithmException;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;

import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.dao.IProdiagsTaskParamsDAO;
import com.ge.healthcare.autosc.common.database.pojo.CfgProdiagsTaskParams;
import com.ge.healthcare.autosc.common.util.ApplicationInfo;
import com.ge.healthcare.autosc.common.util.PropertyConstants;

public class ProdiagsScheduleTask {

	private String taskType = null;
	private String id = null;
	private String taskTypeId = null;
	private boolean first = false;
	private ProdiagsScheduleTask nextTask = null;
	private Hashtable<String, String> taskParams = new Hashtable<String, String>();
	private static IProdiagsTaskParamsDAO prodiagsTaskParamsDAO = null;
	
	public ProdiagsScheduleTask() {}
	
	public void addParam(String name, String value) {
		if(ApplicationInfo.getInstance().getProperty(PropertyConstants.PRODIAGS_TASK_TYPE_IDENTIFIER).equalsIgnoreCase(name)) {
			setTaskType(value);
		} else {
			taskParams.put(name, value);
		}
	}
	
	public Set<String> getParamNames() {
		return (taskParams.keySet());
	}
	
	public String getParamValue(String paramName) {
		return taskParams.get(paramName);
	}
	
	public void setTaskType(String taskType) {
		this.taskType = taskType;
	}
	
	public String getTaskType() {
		return taskType;
	}
	
	public void setId(String id) {
		this.id = id;
	}
	
	public String getId() {
		return id;
	}
	
	public String getTaskTypeId() throws UnknownHostException, NoSuchAlgorithmException {
		if(ASCUtil.isNullOrEmpty(taskTypeId)) {
			taskTypeId = taskType + "_" + id + "_" + ASCUtil.getUID(this);
		}
		return taskTypeId;
	}
	
	public void markFirstTask() {
		first = true;
	}
	
	public boolean isFristTask() {
		return first;
	}
	
	public void setNextTask(ProdiagsScheduleTask nextTask) {
		this.nextTask = nextTask;
	}
	
	public ProdiagsScheduleTask getNextTask() {
		return nextTask;
	}
	
	public boolean isTaskValid() {
		boolean valid = true;
		if(taskType != null) {
			List<CfgProdiagsTaskParams> params = prodiagsTaskParamsDAO.getTaskParamsByTaskName(taskType);
			if(!ASCUtil.isNullOrEmptyList(params)){
				for(CfgProdiagsTaskParams param: params) {
					if(!taskParams.containsKey(param.getParamName().toUpperCase())) {
						// missing required parameter
						valid = false;
						break;
					}
				}
			}
		} else {
			valid = false;
		}
		return valid;
	}
	
	public void setProdiagsTaskParamsDAO(IProdiagsTaskParamsDAO prodiagsTaskParamsDAO) {
		ProdiagsScheduleTask.prodiagsTaskParamsDAO = prodiagsTaskParamsDAO;
	}
	
	public String toString() {
		StringBuffer text = new StringBuffer();
		text.append("{taskName=").append(taskType).append("}");
		Set<String> keys = taskParams.keySet();
		for(String key: keys) {
			text.append("{param name=").append(key).append(",");
			text.append("param value=").append(taskParams.get(key)).append("}");
		}
		return text.toString();
	}
}
